#include "myother.h"

extern char IPADDR[40];//服务器IP地址,  通信用
extern int  PORT;      //服务器IP端口

extern char IPADDR1[40];//服务器IP地址，读卡用
extern int  PORT1;      //服务器IP端口

extern char IPADDR2[40];//服务器IP地址，银联用
extern int  PORT2;      //服务器IP端口

extern char UPPosNum[20];    //银联终端号
extern char UPManageNum[40]; //银联商户号
extern U08  UPMainKey[40];  //银联主密钥

void get_time()
{
	U08 timebuf[8];
	time_t rawtime;
    struct tm* timeinfo;
    char buffer[30];

	time(&rawtime);
    timeinfo = localtime(&rawtime);
    //printf( "It is now: %s", ctime( &time_of_day ) );
    strftime(buffer,sizeof(buffer),"%Y%m%d%H%M%S",timeinfo);
    CurCalc_ASCII_Proc( 2, (U08*)buffer, strlen(buffer),timebuf, 0 );
    memcpy(TradeInformation.DealTime, timebuf, 7);
}

void errmsg( const char *format, ... )
{
    va_list arglist;
    time_t rawtime;
    struct tm* timeinfo;
    char buffer[80];
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    //printf( "It is now: %s", ctime( &time_of_day ) );
    strftime(buffer,sizeof(buffer),"[%Y-%m-%d %H:%M:%S]",timeinfo);
    fprintf( stderr, "%s Warning: ", buffer);
    va_start( arglist, format );
    vfprintf( stderr, format, arglist );
    va_end( arglist );
    if( LogFile != NULL )
    {
       fprintf( LogFile, "%s Warning: ", buffer);
       va_start( arglist, format );
       vfprintf( LogFile, format, arglist );
       va_end( arglist );
       fflush( LogFile );
    }
}

void APP_SysErrProc( void )
{}
// void APP_ComErrProc( void )
// {}
void APP_HookOpenMemory( void )
{
	int rcode =0,i = 0;
	//打开记录文件
	for(i = 0; i < 8; i++)
	{
	    //printf("Open Rec %02d...\r\n",i+1);
	    rcode = APP_Open_RecFile( i+1 );		//
	    if( rcode != 0 )
	    { printf("Open Rec File %02d Fail!\r\n",i+1); }
	    // else
	    // { printf("Open Rec File %02d OK!\r\n",i+1); }
	}
	rcode = APP_OpenBlacklistFile( DEF_BlkMemoryBank_Black );			//打开黑名单文件
	if( rcode != 0 )
	{ printf("Open BlkList File Fail!\r\n"); }
	printf("---------------------------------------------------\r\n");
	//打开用户文件
	rcode = APP_Read_UseFile( DEF_USERID_SysCfg, (U08*)&SysCfg, sizeof( __SysCfg ) );
	if( rcode != 0 )
	{ printf( "Open Use File 01 Fail!\r\n"); }
	// else
	// { printf( "Open Use File 01 OK!\r\n"); }

	rcode = APP_Read_UseFile( DEF_USERID_DealCfg, (U08*)&DealCfg, sizeof( __DealCfg ) );
	if( rcode != 0 )
	{ printf( "Open Use File 02 Fail!\r\n"); }
	// else
	// { printf( "Open Use File 02 OK!\r\n"); }

    rcode = APP_Read_UseFile( DEF_USERID_OthCfg, (U08*)&OthCfg, sizeof( __OthCfg ) );
	if( rcode != 0 )
	{ printf( "Open Use File 03 Fail!\r\n"); }
	// else
	// { printf( "Open Use File 03 OK!\r\n"); }

	TradeInformation.UnitPrice = DealCfg.UnitPrice;	 
	DealCfg.CircuitryID[0] =0x27;
	DealCfg.CircuitryID[1] =0x12;//设置线路

	
	 rcode = APP_Read_UseFile( DEF_USERID_CityID, (U08*)&CityIDCfg, sizeof( __CityIDCfg ) );
	if( rcode != 0 )
	{ printf( "Open Use File 04 Fail!\r\n"); }
	// else
	// { printf( "Open Use File 04 OK!\r\n"); }
 	rcode = APP_Read_UseFile( DEF_USERID_UPBinInfo, (U08*)&UPBinInfo, sizeof( __UPBinInfo ) );
	if( rcode != 0 )
	{ printf( "Open Use File 05 Fail!\r\n"); }
	// else
	// { printf( "Open Use File 05 OK!\r\n"); }

	//====================银联参数
	rcode = APP_UPCash_Init();
	if( rcode != 0 )
	{ printf( "UPCash Init Fail!\r\n"); }
	printf("---------------------------------------------------\r\n");
}

void Init_Memory( void )
{
	int rcode =0,i = 0;
	U08 tbuf[800];
	//创建记录文件
	for(i = 0; i < 8; i++)
	{
	    printf("Init Rec File %02d...\r\n",i+1);
	    rcode = APP_Create_RecFile( i+1, 0,1 );		//创建记录文件 消费
	    if( rcode != 0 )
	    { printf("Init Rec File %02d Fail!\r\n",i+1); }
	    else
	    { printf("Init Rec File %02d OK!\r\n",i+1); }
	}
	//创建黑名单文件
	rcode = APP_CreateBlacklistFile( DEF_BlkMemoryBank_Black );			  //创建黑名单文件
	if( rcode != 0 )
	{  printf("Init BlkList File Fail!\r\n"); }
    else
    {  printf("Init BlkList File OK!\r\n"); }	
	//创建用户文件
	printf("---------------------------------------------------\r\n");
	memset( &SysCfg, 0, sizeof( __SysCfg ) );
	SysCfg.Ver = 1;

	SysCfg.AudioVolume = 10;
	SysCfg.ComType = 'G';
	SysCfg.AppBusSel=2;
	SysCfg.AppBusRec=1;	
	SysCfg.AppBusErrPro = 5;
	SysCfg.NetIPAddr01[0]=127;
	SysCfg.NetIPAddr01[1]=0;
	SysCfg.NetIPAddr01[2]=0;
	SysCfg.NetIPAddr01[3]=1;
	SysCfg.NetIPProt01=5037;
	memcpy(SysCfg.NetAPN01,"cmnet",strlen("cmnet"));
	SysCfg.NetIPAddr02[0]=127;
	SysCfg.NetIPAddr02[1]=0;
	SysCfg.NetIPAddr02[2]=0;
	SysCfg.NetIPAddr02[3]=1;
	SysCfg.NetIPProt02=5050;
	memcpy(SysCfg.NetAPN02,"zgyl.jl",strlen("zgyl.jl"));
    SysCfg.AudioPwmStat=1;
	SysCfg.AudioPwmf=55;
	SysCfg.AudioPwmTim=18;
	SysCfg.NetTimeCont=10;
	SysCfg.NetCoutCont=20;
	SysCfg.NetContTimeOut=15;
	SysCfg.NetTxRxTimeOut=15;
	SysCfg.AppCityID[0]=0x13;
	SysCfg.AppCityID[1]=0x40;//保定城市代码
	SysCfg.AppBusMinBalanc=	500;//余额报警默认5元
	SysCfg.AudioVolume = 20;
	rcode = APP_Write_UseFile( DEF_USERID_SysCfg, (U08*)&SysCfg, sizeof( __SysCfg ) ); 
	if( rcode != 0 )
	{ printf( "Init Use File 01 Fail!\r\n"); }
	else
	{ printf( "Init Use File 01 OK!\r\n"); }

	//=====================
	memset( &DealCfg, 0, sizeof( __DealCfg ) );		 
	DealCfg.Ver	= 1;

	DealCfg.UnitPrice = 100;
	DealCfg.FilialeID =1;
	DealCfg.CircuitryID[0] = 0;
	DealCfg.CircuitryID[1] = 1;
	rcode = APP_Write_UseFile( DEF_USERID_DealCfg, (U08*)&DealCfg, sizeof( __DealCfg ) );
	if( rcode != 0 )
	{ printf( "Init Use File 02 Fail!\r\n"); }
	else
	{ printf( "Init Use File 02 OK!\r\n"); }

	//=====================
	memset( &OthCfg, 0, sizeof( __OthCfg ) );
	OthCfg.MotoState = 1;
	OthCfg.MotoEmpID[0] = 0;	
	OthCfg.MotoEmpID[1] = 0;	
	OthCfg.MotoEmpID[2] = 1;		 
	rcode = APP_Write_UseFile( DEF_USERID_OthCfg, (U08*)&OthCfg, sizeof( __OthCfg ) );
	if( rcode != 0 )
	{ printf( "Init Use File 03 Fail!\r\n"); }
	else
	{ printf( "Init Use File 03 OK!\r\n"); }

   //=====================
	memset( &CityIDCfg, 0, sizeof(__CityIDCfg ) );
			 
	rcode = APP_Write_UseFile( DEF_USERID_CityID, (U08*)&CityIDCfg, sizeof( __CityIDCfg ) );
	if( rcode != 0 )
	{ printf( "Init Use File 04 Fail!\r\n"); }
	else
	{ printf( "Init Use File 04 OK!\r\n"); }

   //=====================
	memset( &UPBinInfo, 0, sizeof( __UPBinInfo ) );	 
	rcode = APP_Write_UseFile( DEF_USERID_UPBinInfo, (U08*)&UPBinInfo, sizeof( __UPBinInfo ) );
	if( rcode != 0 )
	{ printf( "Init Use File 05 Fail!\r\n"); }
	else
	{ printf( "Init Use File 05 OK!\r\n"); }

	//====================银联参数
    UP_PosTlvDataInitMemery( tbuf );
	rcode = APP_Write_SysFile( DEF_USERID_UP_PosTlv	, (U08*)&tbuf, sizeof( __UP_PosTLVSet ) );
	if( rcode != 0 )
	{ printf( "Init Use File 06 Fail!\r\n"); }
	else
	{ printf( "Init Use File 06 OK!\r\n"); }
	
	UP_SysCfgDataInitMemery( tbuf );
	rcode = APP_Write_SysFile( DEF_USERID_UP_SysCfg, (U08*)&tbuf, sizeof( __UP_SystemConfig ) );
	if( rcode != 0 )
	{ printf( "Init Use File 07 Fail!\r\n"); }
	else
	{ printf( "Init Use File 07 OK!\r\n"); }

	UP_CommStatParInit( tbuf );
	rcode = APP_Write_SysFile( DEF_USERID_UP_ComStat, (U08*)&tbuf, sizeof( __UP_CommStatPar ) );
	if( rcode != 0 )
	{ printf( "Init Use File 08 Fail!\r\n"); }
	else
	{ printf( "Init Use File 08 OK!\r\n"); }

}
//=====================================================================
void Load_ConfigIni( void )
{
	char *sect;
	char *key;
	char value[256];
	char *result = NULL;
	int i =0;
	//char stadate[10];
	//char enddate[10];
	//==================加载配置文件
	const char *file = "config.ini";
	Hook_Debug_TxData("    配置信息:         config.ini");
	//printf("load config file %s\n\n", file);
	iniFileLoad(file);
	//加载IP地址和端口
	sect = "IpConfig";
	Hook_Debug_TxData("    采集前置:");
	key  = "IP";
	iniGetString(sect, key, IPADDR, sizeof(IPADDR), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, IPADDR);

	key = "PORT";
	PORT = iniGetInt(sect, key, 5035);
	printf("[%11s] %11s = %d\n", sect, key, PORT);
	//===============
	Hook_Debug_TxData("    读卡服务:");
	key  = "IP1";
	iniGetString(sect, key, IPADDR1, sizeof(IPADDR1), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, IPADDR1);

	key = "PORT1";
	PORT1 = iniGetInt(sect, key, 5035);
	printf("[%11s] %11s = %d\n", sect, key, PORT1);

	//===============
	Hook_Debug_TxData("    银联后台:");
	key  = "IP2";
	iniGetString(sect, key, IPADDR2, sizeof(IPADDR2), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, IPADDR2);

	key = "PORT2";
	PORT2 = iniGetInt(sect, key, 5035);
	printf("[%11s] %11s = %d\n", sect, key, PORT2);
	//===============
	result = strtok( IPADDR2, ".");
    for( i = 0; i < 4; i++)
	{
		if( result != NULL )
		{
			UP_SysCfg.NetUPCashIPAddr[i]=(U08)atoi(result);
			result = strtok( NULL, "." );
		}	
	}
	UP_SysCfg.NetUPCashIPProt =  PORT2;
	
	sect = "OthCfg";
	//加载终端编号
	Hook_Debug_TxData("    终端编号:");
	key  = "PosCode";
	iniGetString(sect, key, value, sizeof(value), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, value);
	CurCalc_ASCII_Proc( 2, (U08*)value, strlen(value),gSamAppInfo.PsamTID, 0 );
	//加载密钥状态
	Hook_Debug_TxData("    接入状态:");
	key  = "KeyStat";
	OthCfg.ComKeyStat = iniGetInt(sect, key, 0);
	printf("[%11s] %11s = %d\n", sect, key, OthCfg.ComKeyStat);
	//加载通信密钥
	key  = "ComKey";
	iniGetString(sect, key, value, sizeof(value), "notfound!");
	//printf("[%11s] %11s = %s\n", sect, key, value);
	CurCalc_ASCII_Proc( 2, (U08*)value, strlen(value),OthCfg.ComKey, 0 );

	//加载银联参数
	Hook_Debug_TxData("  银联终端号:");
	key  = "UPPosNum";
	iniGetString(sect, key, UPPosNum, sizeof(UPPosNum), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, UPPosNum);
	Hook_Debug_TxData("  银联商户号:");
	key  = "UPManageNum";
	iniGetString(sect, key, UPManageNum, sizeof(UPManageNum), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, UPManageNum);
	Hook_Debug_TxData("  银联主密钥:");
	key  = "UPMainKey";
	iniGetString(sect, key, value, sizeof(value), "notfound!");
	printf("[%11s] %11s = %s\n", sect, key, value);
	CurCalc_ASCII_Proc( 2, (U08*)value, strlen(value),UPMainKey, 0 );

	//加载文件信息
	// sect = "RecCfg";
	// key  = "RecName";
	// iniGetString(sect, key, value, sizeof(value), "notfound!");
	// strcpy(Rec_Name,value);
	// printf("[%11s] %11s = %s\n", sect, key, Rec_Name);
	// key  = "StaDate";
	// iniGetString(sect, key, value, sizeof(value), "notfound!");
	// printf("[%11s] %11s = %s\n", sect, key, value);
	// strcpy(stadate,value);
	// key  = "EndDate";
	// iniGetString(sect, key, value, sizeof(value), "notfound!");
	// printf("[%11s] %11s = %s\n", sect, key, value);
	// strcpy(enddate,value);

	//Get_RecNum(Rec_Name,stadate,enddate);

	iniFileFree();
	printf("---------------------------------------------------\r\n");
}

U32 RTC_ReadSysTime( RTC_CLOCK *ptime )				//获取系统时间
{
	get_time();
	ptime->Year_H  =  TradeInformation.DealTime[0];
    ptime->Year_L =   TradeInformation.DealTime[1];
    ptime->Month =    TradeInformation.DealTime[2];
    ptime->Day =      TradeInformation.DealTime[3];
    ptime->Hour =     TradeInformation.DealTime[4];
    ptime->Minute =   TradeInformation.DealTime[5];
    ptime->Second =   TradeInformation.DealTime[6];	
	return 0;
}

U32 RTC_WriteSysTime( RTC_CLOCK *ptime )			//更新系统时间
{
	return 0;
}

U32 HW_RSA_Init( void )
{
	return 0;
}

//===============================================
//==============================================
U32 APP_Save_CfgOth( U32 stat )
{
	U32 rcode=0;
	char *sect;
	char *key;
	char value[256];
	const char *file = "config.ini";
	printf("load file %s\n\n", file);
	iniFileLoad(file);
	sect = "OthCfg";
	key  = "KeyStat";
	printf("[%s] %s = %d\n", sect, key, OthCfg.ComKeyStat);
	iniSetInt(sect, key, OthCfg.ComKeyStat, 10);
	key  = "ComKey";
	sprintf(value,"%02x%02x%02x%02x%02x%02x%02x%02x",OthCfg.ComKey[0],OthCfg.ComKey[1],OthCfg.ComKey[2],OthCfg.ComKey[3]
		,OthCfg.ComKey[4],OthCfg.ComKey[5],OthCfg.ComKey[6],OthCfg.ComKey[7]);
	iniSetString(sect, key, value);
	
	iniFileFree();

	return rcode;
}
U32 APP_Save_OthCfg( U32 stat )
{
	U32 rcode=0;
	rcode = APP_Write_UseFile( DEF_USERID_OthCfg, (U08*)&OthCfg, sizeof( __OthCfg ));
	if(rcode != 0)
	{ 
		DEF_SysCom_Debug;
		printf("Save OthCfg Fail!\r\n");
		return 1;
	}
	else
	{ printf("Save OthCfg OK!\r\n");}
	return 0;
}
U32 APP_Save_DealCfg( U32 stat )
{
	U32 rcode=0;
	rcode = APP_Write_UseFile( DEF_USERID_DealCfg, (U08*)&DealCfg, sizeof( __DealCfg ));
	if(rcode != 0)
	{ 
		DEF_SysCom_Debug;
		printf("Save DealCfg Fail!\r\n");
		return 1;
	}
	else
	{ printf("Save DealCfg OK!\r\n");}
	return 0;
}

U32 APP_Save_SysCfg( U32 stat )
{
	U32 rcode=0;
	rcode = APP_Write_UseFile( DEF_USERID_SysCfg, (U08*)&SysCfg, sizeof( __SysCfg ));
	if(rcode != 0)
	{ 
		DEF_SysCom_Debug;
		printf("Save SysCfg Fail!\r\n");
		return 1;
	}
	else
	{ printf("Save SysCfg OK!\r\n");}
	return 0;
}

void Com_Hook_DebugOutHex( unsigned char *data, unsigned int datalen )
{
    unsigned int i=0;
    for(i =0; i < datalen; i++)
    {
        printf("%02x",data[i]);
    }
    printf("\r\n");
}

void Com_Hook_DebugOutAscii( unsigned char *data, unsigned int datalen )
{
    printf("%s\n",data);
}

void Hook_Debug_TxData( char *tx_buf )
{
	char disbuf[160]={0};
	u2g(tx_buf,sizeof(disbuf),disbuf,sizeof(disbuf));
	printf("%s\r\n",disbuf);
	//SysDebug_TxData(tx_buf,strlen((char*)tx_buf));	
}